import sys
import os.path 

cur_path = os.path.dirname(__file__)
parent_path = os.path.abspath(os.path.join(cur_path, '..'))
sys.path.append(parent_path)

import math
import pymongo
from topapi import *
from exceptions import Exception

def store2db(db, items):
    for item in items:
        item['_id'] = item['num_iid']
        db.items.save(item)

def get_items_and_store_to_db(items_obj, db, user):
    page_no = 1
    page_size = 200
    items_num = 0
    while True:
        items_info = items_obj.onsale_get(user['top_session'], page_no=page_no, page_size=page_size)
        total_results = items_info['total_results']
        items_info = items_info['items']['item']
        items_num += len(items_info)
        store2db(db, items_info)
        if items_num >= total_results:
            break
        page_no += 1

def test_get_items_and_store_to_db():
    m = pymongo.Connection('127.0.0.1:57324')
    db = m.test 
    items_obj = Items()
    user = {
            'top_session':'6100830108c6629bc87406b2456bbc8d782cfc175620c68420839839',
    }
    get_items_and_store_to_db(items_obj, db, user)

def traverse_user_and_get_items():
    m = pymongo.Connection('127.0.0.1:57324')
    db = m.taogold
    skip = 0
    limit = 100 
    items_obj = Items()
    while True:
        cur = db.user.find(fields = ['top_session', 'status'], timeout=False).skip(skip).limit(limit)
        if cur.count(True) <=0:
            break
        for u in cur:
            try:
                if u['status'] != 'normal':
                    print 'user is not normal: %s' %(repr(u['_id']))
                    continue
                get_items_and_store_to_db(items_obj, db, u)
            except Exception as e:
                print 'error in processing: %s, %s' %(repr(u['_id']), repr(e))
                continue 
            print 'processed: %s' %(repr(u['_id']))
        skip += limit

if __name__ == '__main__':
    test_get_items_and_store_to_db()
    #traverse_user_and_get_items()



